package com.zxx.autohomenavi.page_com;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.util.Log;

import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.zxx.autohomenavi.R;
import com.zxx.autohomenavi.base.BaseActivity;

/**
 * 适用不同的Fragment
 */
public class CommonFragmentActivity extends BaseActivity {

    private SegmentTabLayout tabLayout;
    private FragmentManager fragmentManager;
    private Fragment[] fragments;
    private int[] mFragIds = new int[]{R.id.fragment_heart, R.id.fragment_breath,
            R.id.fragment_off_bed, R.id.fragment_turn_over};
    private String[] titles;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_common_fragment;
    }

    @Override
    protected void initView() {
        tabLayout = findViewById(R.id.segment_tab_layout);

        titles = new String[]{"心率", "呼吸", "离床", "体动"};
        tabLayout.setTabData(titles);

        fragmentManager = getSupportFragmentManager();
        fragments = new Fragment[]{fragmentManager.findFragmentById(mFragIds[0]),
                fragmentManager.findFragmentById(mFragIds[1]),
                fragmentManager.findFragmentById(mFragIds[2]),
                fragmentManager.findFragmentById(mFragIds[3]),};

        tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                Log.e("Tag-->", "位置：" + position);

                for (int i = 0; i < fragments.length; i++) {
                    fragmentManager.beginTransaction().hide(fragments[i])
                            .commit();
                }
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                transaction.show(fragments[position])
                        .commit();

            }

            @Override
            public void onTabReselect(int position) {
                Log.e("Tag-->", "re位置：" + position);
            }
        });

    }

}